package chatcontrollers;
import interfaces.ChatController;
import interfaces.ChatModel;
import interfaces.ChatView;

import java.util.Observable;
import java.util.Observer;



public class DefaultChatController implements ChatController{
	
	private ChatView view;
	private ChatModel model;
	
	public void start(){
		view.addNewInputListener(new MessageReadyObserver());
		model.addInputReceivedListener(new MessageReceivedObserver());		
		model.waitForMessages();
		view.waitForMessages();
	}
	
	
	private class MessageReadyObserver implements Observer {
		public void update(Observable obs, Object arg) {
			String input = view.getInput();
			model.sendMessage(input);
		}
	}
	
	
	private class MessageReceivedObserver implements Observer {
		public void update(Observable obs, Object arg) {
			view.showMessage(model.getMessage());
		}
	}
	

	public void setView(ChatView view) {
		this.view = view;
		
	}


	public void setModel(ChatModel model) {
		this.model = model;
		
	}
	
	
}
